/*
 * IBP - Technology architecture for real-time transaction
 * author	: calvin
 * email	: calvinwilliams@163.com
 *
 * Licensed under the LGPL v2.1, see the file LICENSE in base directory.
 */

#include "ibcmd_in.h"

int InitConfigFiles()
{
	char		pathname[ IBP_MAXLEN_FILENAME + 1 ] ;
	
	char		pathfilename[ IBP_MAXLEN_FILENAME + 1 ] ;
	char		file_content[ 4096 + 1 ] ;
	int		file_len ;
	
	int		nret = 0 ;
	
	memset( pathname , 0x00 , sizeof(pathname) );
	snprintf( pathname , sizeof(pathname)-1 , "%s/etc" , getenv("HOME") );
	if( access( pathname , X_OK ) == 0 )
	{
		printf( "WARN : %s exist !\n" , pathname );
	}
	else
	{
		mkdir( pathname , 0755 );
		printf( "mkdir %s ok\n" , pathname );
	}
	
	memset( pathfilename , 0x00 , sizeof(pathfilename) );
	snprintf( pathfilename , sizeof(pathfilename)-1 , "%s/etc/ibcmd.deny" , getenv("HOME") );
	if( access( pathfilename , R_OK ) == 0 )
	{
		printf( "WARN : %s exist !\n" , pathfilename );
	}
	else
	{
		memset( file_content , 0x00 , sizeof(file_content) );
		file_len = snprintf( file_content , sizeof(file_content)-1 , ";" "\n"
										"||" "\n"
										"&&" "\n" );
		
		nret = WriteEntireFile( pathfilename , file_content , file_len ) ;
		if( nret )
		{
			printf( "WriteEntireFile[%s] failed[%d]\n" , pathfilename , nret );
			return HTTP_INTERNAL_SERVER_ERROR;
		}
		
		printf( "%s created\n" , pathfilename );
	}
	
	memset( pathfilename , 0x00 , sizeof(pathfilename) );
	snprintf( pathfilename , sizeof(pathfilename)-1 , "%s/etc/ibcmd.allow" , getenv("HOME") );
	if( access( pathfilename , R_OK ) == 0 )
	{
		printf( "WARN : %s exist !\n" , pathfilename );
	}
	else
	{
		memset( file_content , 0x00 , sizeof(file_content) );
		file_len = snprintf( file_content , sizeof(file_content)-1 , "ls*" "\n"
										"cat*" "\n"
										"ib*" "\n" );
		
		nret = WriteEntireFile( pathfilename , file_content , file_len ) ;
		if( nret )
		{
			printf( "WriteEntireFile[%s] failed[%d]\n" , pathfilename , nret );
			return HTTP_INTERNAL_SERVER_ERROR;
		}
		
		printf( "%s created\n" , pathfilename );
	}
	
	memset( pathname , 0x00 , sizeof(pathname) );
	snprintf( pathname , sizeof(pathname)-1 , "%s/log" , getenv("HOME") );
	if( access( pathname , X_OK ) == 0 )
	{
		printf( "WARN : %s exist !\n" , pathname );
	}
	else
	{
		mkdir( pathname , 0755 );
		printf( "mkdir %s ok\n" , pathname );
	}
	
	memset( pathname , 0x00 , sizeof(pathname) );
	snprintf( pathname , sizeof(pathname)-1 , "%s/file" , getenv("HOME") );
	if( access( pathname , X_OK ) == 0 )
	{
		printf( "WARN : %s exist !\n" , pathname );
	}
	else
	{
		mkdir( pathname , 0755 );
		printf( "mkdir %s ok\n" , pathname );
	}
	
	memset( pathname , 0x00 , sizeof(pathname) );
	snprintf( pathname , sizeof(pathname)-1 , "%s/so" , getenv("HOME") );
	if( access( pathname , X_OK ) == 0 )
	{
		printf( "WARN : %s exist !\n" , pathname );
	}
	else
	{
		mkdir( pathname , 0755 );
		printf( "mkdir %s ok , don't forget copy .so files in it\n" , pathname );
	}
	
	memset( pathname , 0x00 , sizeof(pathname) );
	snprintf( pathname , sizeof(pathname)-1 , "%s/keys" , getenv("HOME") );
	if( access( pathname , X_OK ) == 0 )
	{
		printf( "WARN : %s exist !\n" , pathname );
	}
	else
	{
		mkdir( pathname , 0755 );
		printf( "mkdir %s ok , don't forget generate keys\n" , pathname );
	}
	
	return HTTP_OK;
}

